unit uAddAgreementWindowOtliv;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, ToolWin, Grids, DBGridEh, StdCtrls, DBCtrls,
  Mask, DBCtrlsEh, DBLookupEh, ImageTools;

type
  TfAddAgreementWindowOtliv = class(TForm)
    Label1: TLabel;
    DBLookupComboboxEh1: TDBLookupComboboxEh;
    GroupBox3: TGroupBox;
    Image1: TImage;
    DBNavigator1: TDBNavigator;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    GroupBox1: TGroupBox;
    DBGridEh1: TDBGridEh;
    tbWindowSize: TToolBar;
    tbAddWindowSize: TToolButton;
    tbDelWindowSize: TToolButton;
    DBEditEh1: TDBEditEh;
    Label2: TLabel;
    procedure DBLookupComboboxEh1Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure tbAddWindowSizeClick(Sender: TObject);
    procedure tbDelWindowSizeClick(Sender: TObject);
    procedure DBGridEh1KeyPress(Sender: TObject; var Key: Char);
  private
    AgreementWindowId: Integer;
  public
    { Public declarations }
  end;

var
  fAddAgreementWindowOtliv: TfAddAgreementWindowOtliv;

implementation

uses uDBData, uDBCommand;

{$R *.dfm}

procedure TfAddAgreementWindowOtliv.DBLookupComboboxEh1Change(
  Sender: TObject);
begin
  fDBData.dOtliv.DataSet.Filtered := False;
  fDBData.dOtliv.DataSet.Filter := 'OtlivGroupId='+fDBData.dOtlivGroup.DataSet.FieldByName('OtlivGroupId').AsString;
  fDBData.dOtliv.DataSet.Filtered := True;
  Image1.Picture.Bitmap.Assign(HImageTools.GetBitmap(fDBData.dOtliv, 'OtlivImage'));
end;

procedure TfAddAgreementWindowOtliv.Button3Click(Sender: TObject);
begin
  if(Button1.Enabled = False) then fDBData.DeleteRecord(fDBData.dAgreementWindowOtliv);
  fDBData.dOtliv.DataSet.Filtered := False;
  fDBData.dOtliv.DataSet.Filter := '';
  Close();
end;

procedure TfAddAgreementWindowOtliv.Button2Click(Sender: TObject);
begin
  fDBData.dOtliv.DataSet.Filtered := False;
  fDBData.dOtliv.DataSet.Filter := '';
  Close();
end;

procedure TfAddAgreementWindowOtliv.DBNavigator1Click(Sender: TObject;
  Button: TNavigateBtn);
begin
  Image1.Picture.Bitmap.Assign(HImageTools.GetBitmap(fDBData.dOtliv, 'OtlivImage'));
end;

procedure TfAddAgreementWindowOtliv.Button1Click(Sender: TObject);
begin
  if(fDBData.dOtlivGroupSizePosition.DataSet.RecordCount > 0)then
  begin
    fDBData.dAgreementWindowOtliv.DataSet.FieldByName('AgreementWindowId').Value := AgreementWindowId;
    fDBData.dAgreementWindowOtliv.DataSet.FieldByName('OtlivId').Value := fDBData.dOtliv.DataSet.FieldByName('OtlivId').AsInteger;
    fDBData.dAgreementWindowOtliv.DataSet.Post();
    while(not fDBData.dOtlivGroupSizePosition.DataSet.Eof)do
    begin
      fDBCommand.qAddAgreementWindowOtlivSizePosition.ParamByName('IDAgreementWindowOtliv').Value := fDBData.qAgreementWindowOtliv.InsertId;
      fDBCommand.qAddAgreementWindowOtlivSizePosition.ParamByName('IDSizePosition').Value := fDBData.dOtlivGroupSizePosition.DataSet.FieldByName('SizePositionId').AsInteger;
      fDBCommand.qAddAgreementWindowOtlivSizePosition.Execute();
      fDBData.dOtlivGroupSizePosition.DataSet.Next();
    end;
    fDBData.qAgreementWindowOtlivSizePosition.Active := False;
    fDBData.qAgreementWindowOtlivSizePosition.ParamByName('IDAgreementWindowOtliv').Value := fDBData.qAgreementWindowOtliv.InsertId;
    fDBData.qAgreementWindowOtlivSizePosition.Active := True;
    Button1.Enabled := false;
    Button2.Enabled := true;
  end;
end;

procedure TfAddAgreementWindowOtliv.FormShow(Sender: TObject);
begin
  Button1.Enabled := True;
  Button2.Enabled := False;
  if(fDBData.qAgreementWindow.InsertId > 0) then
  begin
    AgreementWindowId := fDBData.qAgreementWindow.InsertId;
  end else
  begin
    AgreementWindowId := fDBData.dAgreementWindow.DataSet.FieldByName('AgreementWindowId').AsInteger;
  end;
end;

procedure TfAddAgreementWindowOtliv.tbAddWindowSizeClick(Sender: TObject);
begin
  fDBData.InsertRecord(fDBData.dAgreementWindowOtlivSizePosition);
end;

procedure TfAddAgreementWindowOtliv.tbDelWindowSizeClick(Sender: TObject);
begin
  fDBData.DeleteRecord(fDBData.dAgreementWindowOtlivSizePosition);
end;

procedure TfAddAgreementWindowOtliv.DBGridEh1KeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = #13 then (Sender as TDBGridEh).DataSource.DataSet.Post;
end;

end.
